﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using Website.Logger;
using DAL;
using BOL;
using WebLib;
using System.Web.Script.Services;
using System.Web.Services;
using UtilityExtensions;

namespace UILGestion.Admin
{
    public partial class TipoCambioEditForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                 // aqui de de ser por empresa  
                    internalid.Value = Request.Params["id"];
                    type.Value = (string)Request.QueryString["type"];
                    internalid.Value = Request.Params["id"];
                    type.Value = type.Value == "" ? "A" : type.Value;
                    menuCtrl1.loadMenuAdmin();
                   FormatForm();

                }
                catch (SqlException sqlex)
                {
                    WebServerSideSqlError.Log(sqlex);

                    ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                     "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


                }
                catch (Exception ex)
                {
                    WebServerSideError.Log(ex);
                    ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                         "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

                }

            }
        }
        protected void btnContinue_OnClick(object sender, EventArgs e)
        {
            saveButton.Enabled = false;
            try
            {

                if ((sender as Button).Text == "Guardar")
                {
                    //(DD/MM/yyyy)
                    System.IFormatProvider MiFp =
                        new System.Globalization.CultureInfo("es-ES", false);
                    tipoCambioBL tbol = new tipoCambioBL();

                       if (type.Value=="A")
                       {
                           internalid.Value = tbol.Add(
                                    DateTime.ParseExact(fechaInsertTextBox.Text, "dd/MM/yyyy", MiFp),
                                    int.Parse(monedaBaseDropDownList.SelectedValue),
                                    int.Parse(monedaCambioDropDownList.SelectedValue),
                                    decimal.Parse(tipoCambioTextBox.Text),
                                    decimal.Parse(tipoCambioCompraTXT.Text)).ToString();
                       }
                       else
                       {
                           
                                tbol.Update(int.Parse(internalid.Value),
                                DateTime.ParseExact(fechaInsertTextBox.Text, "dd/MM/yyyy", MiFp),
                                int.Parse(monedaBaseDropDownList.SelectedValue),
                                int.Parse(monedaCambioDropDownList.SelectedValue),
                                decimal.Parse(tipoCambioTextBox.Text),
                                decimal.Parse(tipoCambioCompraTXT.Text));

                       }
                    
                    
                    Response.Redirect("TipoCambioReadForm.aspx?id=" + internalid.Value, true);
                }
                if ((sender as Button).Text == "Cancelar")
                {
                    Response.Redirect("TipoCambioListForm.aspx");
                }
                if ((sender as Button).Text == "refreshButton")
                {
                    Response.Redirect("TipoCambioEditForm.aspx?type={0}&id={1}".Fmt(type.Value,internalid.Value), true);
                    type.Value = type.Value == "" ? "A" : type.Value;
                    menuCtrl1.loadMenuAdmin();
                }


            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);

                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                     "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }




        }
        private void ShowAllComboBox()
        {
            
            var q = from i in DbUtil.Db.Monedas
                    select i;


            monedaBaseDropDownList.DataTextField = "descripcion";
            monedaBaseDropDownList.DataValueField = "idInternal";

            monedaBaseDropDownList.DataSource = q;
            monedaBaseDropDownList.DataBind();
            monedaBaseDropDownList.Enabled = false;


            monedaCambioDropDownList.DataTextField = "descripcion";
            monedaCambioDropDownList.DataValueField = "idInternal";

            monedaCambioDropDownList.DataSource = q;
            monedaCambioDropDownList.DataBind();





        }

        private DAL.TipoCambio PopulateObjectInsert(DAL.TipoCambio entity )
        {
            //(DD/MM/yyyy)
            System.IFormatProvider MiFp =
                new System.Globalization.CultureInfo("es-ES", false);


           
            entity.fecha =
                DateTime.ParseExact(fechaInsertTextBox.Text, "dd/MM/yyyy", MiFp);
           // entity.idInternal = int.Parse(internalid.Value);
            //entity.idEmpresa = 1;
            entity.idMonedaBase = int.Parse(monedaBaseDropDownList.SelectedValue);
            entity.idMonedaCambio = int.Parse(monedaCambioDropDownList.SelectedValue);
            entity.monto = decimal.Parse(tipoCambioTextBox.Text);

            return entity;
        }
        private void editObject()
        {
            
            DAL.TipoCambio tp = DbUtil.Db.TipoCambios.Single(p => p.idInternal == int.Parse(internalid.Value));

            
            fechaInsertTextBox.Text=tp.fecha.Value.ToShortDateString();
                tipoCambioTextBox.Text=tp.monto.Value.ToString();
                monedaBaseDropDownList.SelectedValue=tp.idMonedaBase.ToString();
                monedaCambioDropDownList.SelectedValue = tp.idMonedaCambio.ToString();
                tipoCambioCompraTXT.Text = tp.montoCompra.ToString();
               

        }

        private void FormatForm()
        {
            //--------------------
            int idEmpleadoEmisor = int.Parse(System.Web.HttpContext.Current.Session["idEmpleado"].ToString());
            bool acceso = new DAL.FormularioAcceso().VerificarAcceso(DbUtil.Db,
                idEmpleadoEmisor,
                12, //tipoCambio
                3); //Crear
            //------------------------- 
            // los paneles presentes en el formulario
            PanelBody.Visible = true;
            PanelButtom.Visible = true;

            fechaInsertTextBox.Text = DateTime.Now.ToShortDateString();
           //         string Lang = "es-ES";  //para controlar el idioma 
           //         System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo
           //(Lang);

          ShowAllComboBox();
          if (type.Value == "E")
          {
              editObject();
          }
          else
          {
              monedaCambioDropDownList.SelectedValue = "4";
              monedaCambioDropDownList.Enabled = false;
          }


           
            //restricionesForm();


            //AddPartidaButton.OnClientClick =
            //  WindowPartida.
            //  GetShowReference("../ObraAdmin/PartidaAdicionInsertForm.aspx?id=0&accion=Insert&ds=" + idobjectdsLabel.Text.ToString(), "Registro de Partida") + "return false;";

        }

        protected void refreshPartida_Click(object sender, EventArgs e)
        {
            try
            {
                FormatForm();
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);

                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                     "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }
            

        }

       

        
       
    }
}